package com.maskota;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Alimento extends Activity {
	LinearLayout lperros,lgatos,lpeces,lhuron,lroedor,lrepti,lave;
	ImageView iperros,igatos,ipeces,ihuron,iroedor,ireptil,iave,
	iperros2,igatos2,ipeces2,ihuron2,iroedor2,irepti2,iave2 ;
	TextView tperro1,tperro2,tgato1,tgato2,tpez1,tpez2,thuron1,
	thuron2,troedor1,troedor2,treptil1,treptil2,tave1,tave2;
	
	Button aperro,agato,apez,ahuron,aroedor,areptil,aave;
	SharedPreferences preferences;
	SharedPreferences.Editor editor;
	String seleccion;
	int numproductos=0;
	public UsuariosSQLiteHelper usdbh;
    public SQLiteDatabase db;
//	String url="http://maskota.zedtrix.com/img/productos/";
	String[] arrayid,arrayprod,arrayprecio,arrayurl;
	TextView textalimento, textoperro,textogato,textohuron,textoave,textoreptil,textoroedor,textopez;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.alimento);
		
		//Texto
		
			Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/vagroundedbold.ttf");
			textalimento = (TextView) findViewById(R.id.textalimento);
			textoperro = (TextView) findViewById(R.id.textperros);
			textogato = (TextView) findViewById(R.id.textogatos);
			textohuron = (TextView) findViewById(R.id.textohurones);
			textoave = (TextView) findViewById(R.id.textoaves);
			textoreptil = (TextView) findViewById(R.id.textoreptiles);
			textoroedor = (TextView) findViewById(R.id.textoroedores);
			textopez=(TextView) findViewById(R.id.textopeces);
			textalimento.setTypeface(tf);
			textoperro.setTypeface(tf);
			textogato.setTypeface(tf);
			textohuron.setTypeface(tf);
			textoave.setTypeface(tf);
			textoreptil.setTypeface(tf);
			textoroedor.setTypeface(tf);
			textopez.setTypeface(tf);
			
		
	    usdbh = new UsuariosSQLiteHelper(this, "Maskota", null, 1);

		preferences=getSharedPreferences("MasKotaPreferencias", MODE_PRIVATE);
		editor=preferences.edit();
		
		
		verificar();
		
		
		for(int i=0;i<arrayid.length;i++){
			Log.i("arrayid["+i+"]", arrayid[i]);
		}
		for(int i=0;i<arrayprod.length;i++){
			Log.i("arrayprod["+i+"]", arrayprod[i]);
		}
		for(int i=0;i<arrayprecio.length;i++){
			Log.i("arrayprecio["+i+"]", arrayprecio[i]);
		}
		for(int i=0;i<arrayurl.length;i++){
			Log.i("arrayurl["+i+"]", arrayurl[i]);
		}
		

		     	          
        		
		seleccion=preferences.getString("mibuilder", ""+0);
		String[] arrayanimales=seleccion.split(",");
		

		
		
		lperros=(LinearLayout) findViewById(R.id.layoutperros);
		lperros.setVisibility(View.GONE);
		lgatos=(LinearLayout) findViewById(R.id.layoutgatos);
		lgatos.setVisibility(View.GONE);
		lhuron=(LinearLayout) findViewById(R.id.layouthurones);
		lhuron.setVisibility(View.GONE);
		lroedor=(LinearLayout) findViewById(R.id.layoutratones);
		lroedor.setVisibility(View.GONE);
		lrepti=(LinearLayout) findViewById(R.id.layoutreptiles);
		lrepti.setVisibility(View.GONE);
		lave=(LinearLayout) findViewById(R.id.layoutaves);
		lave.setVisibility(View.GONE);
		lpeces=(LinearLayout) findViewById(R.id.layoutpeces);
		lpeces.setVisibility(View.GONE);
		
		aperro=(Button) findViewById(R.id.alimentosperro);
		aperro.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {


				
				editor.putString("vartipoproducto", "getAlimento&productos=1");
				editor.commit();
				
				
				Intent goAlimentos=new Intent(Alimento.this, DetalleProductos.class);
				startActivity(goAlimentos);

			}
		});
		

		
		
		
		agato=(Button) findViewById(R.id.alimentosgato);
		apez=(Button) findViewById(R.id.alimentospeces);
		ahuron=(Button) findViewById(R.id.alimentoshurones);
		aroedor=(Button) findViewById(R.id.alimentosroedores);
		areptil=(Button) findViewById(R.id.alimentosreptiles);
		aave=(Button) findViewById(R.id.alimentosaves);
		
		agato.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				editor.putString("vartipoproducto", "getAlimento&productos=2");
				editor.commit();
				Intent goAlimentos=new Intent(Alimento.this, DetalleProductos.class);
				startActivity(goAlimentos);				
			}
		});
		apez.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				editor.putString("vartipoproducto", "getAlimento&productos=3");
				editor.commit();
				Intent goAlimentos=new Intent(Alimento.this, DetalleProductos.class);
				startActivity(goAlimentos);					
			}
		});
		ahuron.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				editor.putString("vartipoproducto", "getAlimento&productos=4");
				editor.commit();
				Intent goAlimentos=new Intent(Alimento.this, DetalleProductos.class);
				startActivity(goAlimentos);					
			}
		});
		aroedor.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				editor.putString("vartipoproducto", "getAlimento&productos=5");
				editor.commit();
				Intent goAlimentos=new Intent(Alimento.this, DetalleProductos.class);
				startActivity(goAlimentos);					
			}
		});
		areptil.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				editor.putString("vartipoproducto", "getAlimento&productos=6");
				editor.commit();
				Intent goAlimentos=new Intent(Alimento.this, DetalleProductos.class);
				startActivity(goAlimentos);					
			}
		});
		aave.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				editor.putString("vartipoproducto", "getAlimento&productos=7");
				editor.commit();
				Intent goAlimentos=new Intent(Alimento.this, DetalleProductos.class);
				startActivity(goAlimentos);					
			}
		});

		
		for(int i=0;i<arrayanimales.length;i++){
			if(arrayanimales[i].equals("1")){
				lperros.setVisibility(View.VISIBLE);	
			}
			
			if(arrayanimales[i].equals("2")){
				lgatos.setVisibility(View.VISIBLE);	
			}
			if(arrayanimales[i].equals("3")){
				lpeces.setVisibility(View.VISIBLE);	
			}
			if(arrayanimales[i].equals("4")){
				lhuron.setVisibility(View.VISIBLE);	
			}
			if(arrayanimales[i].equals("5")){
				lroedor.setVisibility(View.VISIBLE);	
			}
			if(arrayanimales[i].equals("6")){
				lrepti.setVisibility(View.VISIBLE);	
			}
			if(arrayanimales[i].equals("7")){
				lave.setVisibility(View.VISIBLE);	
			}
		}
		
		

		
	
	}
	
	
    private void verificar() {

	    db = usdbh.getWritableDatabase();
	    if(db != null)
	    {
	    
	        Cursor c = db.rawQuery("SELECT * FROM Articulos", null);
	        
	        arrayid=new String[c.getCount()];
	        arrayprod=new String[c.getCount()];
	        arrayprecio=new String[c.getCount()];
	        arrayurl=new String[c.getCount()];
	        int cont=0;
	    	if (c.moveToFirst()) {
	    	     do {
	    	    	  int id=c.getInt(0);
	    	          String articulo = c.getString(1);
	    	          Float precio = c.getFloat(2);
	    	          String urlfoto=c.getString(3);
	    	          
	    	          arrayid[cont]=""+id;
	    	          arrayprod[cont]=articulo;
	    	          arrayprecio[cont]=""+precio;
	    	          arrayurl[cont]=urlfoto;
	    	          

	    	          Log.i("BASE ARTICULOS", "id: "+id+" articulo: "+articulo+" precio: "+precio+" url: "+urlfoto);

	    	          cont++;
	    	          
	    	     } while(c.moveToNext());
	
	
	                       	  
	        }
	
	    	db.close();
	
	    }           
    }
    
 	private Drawable ImageOperations(Context ctx, String url, String saveFilename, String myurl) {
        try {
                InputStream is = (InputStream) this.fetch(myurl);
                Drawable d = Drawable.createFromStream(is, "src");
                return d;
        } catch (MalformedURLException e) {
                e.printStackTrace();
                return null;
        } catch (IOException e) {
                e.printStackTrace();
                return null;
        }
    }

    public Object fetch(String address) throws MalformedURLException,IOException {
        URL url = new URL(address);
        Object content = url.getContent();
        return content;
    }
    
    
    
    private void scaleImage(ImageView view, int boundBoxInDp)
    {
        // Get the ImageView and its bitmap
        Drawable drawing = view.getDrawable();
        Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();

        // Get current dimensions
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        // Determine how much to scale: the dimension requiring less scaling is
        // closer to the its side. This way the image always stays inside your
        // bounding box AND either x/y axis touches it.
        float xScale = ((float) boundBoxInDp) / width;
        float yScale = ((float) boundBoxInDp) / height;
        float scale = (xScale <= yScale) ? xScale : yScale;

        // Create a matrix for the scaling and add the scaling data
        Matrix matrix = new Matrix();
        matrix.postScale(scale, scale);

        // Create a new bitmap and convert it to a format understood by the ImageView
        Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
        BitmapDrawable result = new BitmapDrawable(scaledBitmap);
        width = scaledBitmap.getWidth();
        height = scaledBitmap.getHeight();

        // Apply the scaled bitmap
        view.setImageDrawable(result);

        // Now change ImageView's dimensions to match the scaled image
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
        params.width = width;
        params.height = height;
        view.setLayoutParams(params);
    }

    private int dpToPx(int dp)
    {
        float density = getApplicationContext().getResources().getDisplayMetrics().density;
        return Math.round((float)dp * density);
    }
}
